using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using stsdev.Builders.SolutionFiles;

namespace stsdev
{

    class FeatureWithApplicationPageSolutionProvider : ISolutionProvider
    {
        private ProjectFileBuilder _projectFileBuilder;

        public FeatureWithApplicationPageSolutionProvider()
        {
            _projectFileBuilder = new ProjectFileBuilder();
        }

        public string Title
        {
            get { return "Application Pages with Navigation Feature (C# assembly)"; }
        }

        public void InitializeSolution()
        {
            SolutionBuilder.AssemblyDeployment = true;
            SolutionBuilder.SafeControlSettings = false;
            SolutionBuilder.DeploymentTarget = AssemblyDeploymentTarget.GlobalAssemblyCache;
            SolutionBuilder.CasPermissions = false;
        }

        string[] AppPageNames = { "AppPage1", "AppPage2", "AppPage3" };

        public void AddSolutionItems()
        {

            ImageFileBuilder.Create(Properties.Resources.Binoculars32, "Binoculars32.gif");
            foreach (string AppPageName in AppPageNames)
            {
                ApplicationPageBuilder.Create(AppPageName);
            }
            FeatureBuilder.CreateElementManifest("ApplicationPageNavigation.xml", ElementWriter);
            FeatureBuilder.Create(SolutionBuilder.SolutionName,
                                  FeatureScopeEnum.Site);
        }

        private void ElementWriter(XmlWriter writer)
        {

            int SequenceNumber = 1001;

            foreach (string AppPageName in AppPageNames)
            {
                writer.WriteStartElement("CustomAction");
                writer.WriteAttributeString("Id", AppPageName + "NavigationMenu");
                writer.WriteAttributeString("GroupId", "SiteActions");
                writer.WriteAttributeString("Location", "Microsoft.SharePoint.StandardMenu");
                writer.WriteAttributeString("Sequence", SequenceNumber.ToString());
                writer.WriteAttributeString("Title", AppPageName);
                writer.WriteAttributeString("Description", "Navigate to " + AppPageName);
                writer.WriteStartElement("UrlAction");
                string AppPageUrl = "~sitecollection/_layouts/" + SolutionBuilder.SolutionName + @"/" + AppPageName + ".aspx";
                writer.WriteAttributeString("Url", AppPageUrl);
                writer.WriteEndElement(); // </UrlAction>
                writer.WriteEndElement(); // </CustomAction>  
                SequenceNumber++;
            }
        }


        #region ISolutionProvider Members


        public IProjectFileBuilder ProjectProvider
        {
            get { return _projectFileBuilder; }
        }

        #endregion
    }
}
